/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.particle;

import com.android1.amarena2d.engine.Config;
import com.android1.amarena2d.engine.GL;
import com.android1.amarena2d.engine.Resource;
import com.android1.amarena2d.engine.SpriteBatchInstance;
import com.android1.amarena2d.engine.render.AmarenaSpriteBatch;
import com.android1.amarena2d.nodes.BaseNode;
import com.android1.amarena2d.nodes.particle.effects.ParticleEffect;
import com.android1.amarena2d.texture.ManagedTexture;

public class ParticleSystem extends BaseNode<Emitter> implements Resource.Consumer {

    AmarenaSpriteBatch spriteBatch;
    protected ManagedTexture managedTexture;

    public ParticleSystem(ManagedTexture managedTexture) {
        this(managedTexture, engine.getConfig().get(Config.EMITTER_MAX_PER_SYSTEM));
    }

    /**
     * Overrides the default particle effect textures
     *
     * @param managedTexture Texture replacing the default particel texture
     */
    public ParticleSystem(ManagedTexture managedTexture, final int maxEmitter) {
        ensureChildren(maxEmitter);
        this.managedTexture = managedTexture;
        this.spriteBatch = SpriteBatchInstance.get();
    }


    @Override
    public void init() {
        if (!init) {
            managedTexture.obtainRef(this);
            managedTexture.init();
            super.init();
        }
    }

    @Override
    public void dispose() {
        if (init) {
            managedTexture.releaseRef(this);
            managedTexture.dispose();
            super.dispose();
        }
    }

    @Override
    public void render() {
        if (!init || !active || !visible)
            return;
        final float x = getX();
        final float y = getY();

        final boolean isTranslate = transformDelegate != null || x != 0 || y != 0;

        if (isTranslate)
            GL.pushMatrix();

        GL.setColor(this.color);

        if (x != 0 || y != 0)
            GL.translate(x, y);

        if (transformDelegate != null) {
            transformDelegate.apply();
        }

        if (children != null) {
            spriteBatch.begin();
            onRender();
            renderChilds();
            spriteBatch.end();
            onRenderEnd();
        }

        if (isTranslate)
            GL.popMatrix();

        GL.invalidateColor();
    }


    @Override
    public void setRenderChildsFirst(boolean renderChildsFirst) {
        throw new UnsupportedOperationException("Not useable in a ParticleSystem.");
    }

    public Emitter createEmitter(ParticleEffect effect, float x, float y) {
        Emitter emitter = EmitterFactory.create(effect, x, y);
        add(emitter);
        return emitter;
    }

    public Emitter createEmitter(ParticleEffect effect, float x, float y, float width, float height) {
        Emitter emitter = EmitterFactory.create(effect, x, y, width, height);
        add(emitter);
        return emitter;
    }

    public ManagedTexture getManagedTexture() {
        return managedTexture;
    }

//    @Override
//    public float getAbsoluteX() {
//        return parent.getAbsoluteX();
//    }
//
//    @Override
//    public float getAbsoluteY() {
//        return parent.getAbsoluteY();
//    }
//
//    @Override
//    public void setAbsoluteX(float x) {
//        throw new UnsupportedOperationException();
//    }
//
//    @Override
//    public void setAbsoluteY(float y) {
//        throw new UnsupportedOperationException();
//    }
//
//    @Override
//    public float getWidth() {
//        return 0;
//    }
//
//    @Override
//    public float getHeight() {
//        return 0;
//    }
//
//    @Override
//    public float getX() {
//        return 0;
//    }
//
//    @Override
//    public float getY() {
//        return 0;
//    }
//
//    @Override
//    public void setX(float x) {
//        throw new UnsupportedOperationException();
//    }
//
//    @Override
//    public void setY(float y) {
//        throw new UnsupportedOperationException();
//    }
//
//    @Override
//    public void setXY(float x, float y) {
//
//    }
//
//    @Override
//    public Vector2 getXY() {
//        return null;
//    }


}
